using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class GetContent : System.Web.UI.Page
{
    private BusinessLayer.IIndexingService indexingService;

    public BusinessLayer.IIndexingService IndexingService
    {
        get { return indexingService; }
        set { indexingService = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CreatedUrls"] != null)
        {
            ArrayList urls = (ArrayList)Session["CreatedUrls"];

            foreach (UrlData ud in urls)
            {
                ResponseParser parser = new ResponseParser(ud.Url, Server.MapPath("~/Config"));
                getUrls(parser, 0, ud.Level);
            }
        }
    }

    private void getUrls(ResponseParser parser, int level, int maxLevel)
    {
        ArrayList urls = parser.Links();
        int nextLevel = level + 1;

        if (nextLevel <= maxLevel)
        {
            foreach (string s in urls)
            {
                try
                {
                    ResponseParser reqParser = new ResponseParser(s, Server.MapPath("~/Config"));
                    string content = reqParser.getText();

                    if (!content.Equals(string.Empty))
                    {
                        indexingService.IndexContent(reqParser.Url, content);
                    }

                    getUrls(reqParser, nextLevel, maxLevel);
                }
                catch (Exception e)
                {
                    Trace.Write(e.Message + " in " + s);
                }
            }
        }
        else
        {
            return;
        }

    }
}
